package com.jic.market.impl.allocation;

import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.market.common.enums.CostAllocationFlagEnum;
import com.jic.market.entity.AllocationModel;
import com.jic.market.request.PromotionRequest;
import com.jic.market.request.after.TellAddAllocationRequest;
import com.jic.market.response.PromotionResponse;
import com.jic.market.service.JicMarketPromotionService;
import com.jic.market.service.allocation.PromotionAddAllocationStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xuyongliang
 * @version V1.0
 * @className PromotionAddAllocationStrategyNormal
 * @description 通用活动分摊策略
 * @date 2020/3/16
 */
@Slf4j
@Service("allocationNormal")
public class PromotionAddAllocationStrategyNormal implements PromotionAddAllocationStrategy {

    @Resource
    private JicMarketPromotionService jicMarketPromotionService;


    @Override
    public AllocationModel collectionAllocation(TellAddAllocationRequest request) {
        log.info("--------进入通用活动分摊策略分摊策略tellRequest--{}-----",request);
        // 转成 long 类型
        Long promotionId =Long.valueOf(request.getPromotionId());
        PromotionRequest promotionRequest = new PromotionRequest();
        promotionRequest.setId(promotionId);
        PromotionResponse detail = jicMarketPromotionService.detail(promotionRequest);

        AllocationModel model =new AllocationModel();
        if (detail.getCostAllocationFlag().equals("1")){
            BeanUtils.copyProperties(detail,model);
            model.setParentAllocationScale(detail.getParentAllocationScale());
            model.setChildAllocationScale(detail.getChildAllocationScale());
            model.setMerchantAllocationScale(detail.getMerchantAllocationScale());
            model.setStoreAllocationScale(detail.getStoreAllocationScale());

        }else {
            //不分摊则判断当前活动组织等级设置默认值100
            if (detail.getDeptType() == DeptTypeEnums.OPERATION_PLATFORM.getCode()){
                model.setParentAllocationScale(100);
            }else if (detail.getDeptType() == DeptTypeEnums.CHILD_PLATFORM.getCode()){
                model.setChildAllocationScale(100);
            }else if (detail.getDeptType() == DeptTypeEnums.MERCHANT_PLATFORM.getCode()){
                model.setMerchantAllocationScale(100);
            }else {
                model.setStoreAllocationScale(100);
            }
        }
        model.setCostAllocationFlag(detail.getCostAllocationFlag());
        model.setPromotionId(detail.getPromotionCode());
        model.setDeptId(detail.getDeptId());
        // 增加deptType 字段
        model.setDeptType(detail.getDeptType());
        model.setPromType(detail.getPromType());
        // CostAllocationFlagEnum.SHARE.getCode().equals(detail.getCostAllocationFlag())
        return model;
    }
}
